package com.kmx.demo.datagen;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kmx.demo.config.Configuration;
import com.kmx.demo.utils.DateUtils;
import com.kmx.demo.utils.K2FileUtils;
import com.kmx.demo.utils.UUID;

public class DemoDataGen {

	public static void main(String[] args) {
		String outputDir = "demoData";
		long startTime = DateUtils.convertIsoToTimestamp("2016-12-01T01:00:00.000+08:00");
		long dataFreq = 30000;
		long duration = 2592000000l;
		long endTime = startTime + duration;
		int fgNum = 1;
		int assetsPerFg = 3;
		String terminator = ",";
		boolean containEx = true;
		boolean genMetadata = true;

		if (args.length == 1 && "configFile".equalsIgnoreCase(args[0])) {
			Help.genConfigFile();
			return;
		}

		if (new File("conf/config.properties").exists() && args.length == 0) {
			Configuration config = new Configuration();
			outputDir = config.getOutputDir();
			fgNum = config.getFgNum();
			assetsPerFg = config.getAssetsNumPerFg();
			startTime = DateUtils.convertIsoToTimestamp(config.getStartTime());
			dataFreq = config.getDataFreq();
			duration = config.getDuration();
			terminator = config.getCsvTerminator();
			endTime = startTime + duration;
			containEx = config.isContainEx();
			genMetadata = config.isGenMetadata();
		}

		if (args.length > 0) {
			if (args.length != 9) {
				System.out.println("input not match the parameters list,please check...");
				return;
			} else {
				try {
					outputDir = args[0];
					genMetadata = Boolean.parseBoolean(args[1]);
					fgNum = Integer.parseInt(args[2]);
					assetsPerFg = Integer.parseInt(args[3]);
					startTime = DateUtils.convertIsoToTimestamp(args[4]);
					duration = DateUtils.convertDurationToMs(args[5]);
					dataFreq = Long.parseLong(args[6]);
					terminator = args[7];
					endTime = startTime + duration;
					containEx = Boolean.parseBoolean(args[8]);
				} catch (Exception e) {
					e.printStackTrace();
					return;
				}

			}

		}

		DemoDataGen.genData(outputDir, genMetadata, fgNum, assetsPerFg, startTime, endTime, dataFreq, terminator,
				containEx);
	}

	public static void genData(String outputDir, boolean genMetadata, int fgNum, int assetsPerFg, long startTime,
			long endTime, long dataFreq, String terminator, boolean containEx) {

		String uuid = null;

		outputDir = K2FileUtils.filePath(outputDir);
		String metadataDir = outputDir + "metadata";
		String dataDir = outputDir + "data";
		if (genMetadata) {
			K2FileUtils.backupAndRebuildDir(outputDir);
			uuid = UUID.get();
			K2FileUtils.mkdirs(metadataDir);
			K2FileUtils.mkdirs(dataDir);
			GenMetaData.genFixedFieldGroup(metadataDir, uuid, fgNum, dataFreq + "");
			GenMetaData.genAssets(metadataDir, uuid, fgNum, assetsPerFg);
		} else {
			K2FileUtils.backupAndRebuildDir(dataDir);
			uuid = getUUIDFromFg(metadataDir + "/fieldGroup");
		}
		GenCsvData.genData(dataDir, uuid, GenMetaData.genFgIdList(uuid, fgNum), assetsPerFg, startTime, endTime,
				dataFreq, terminator, containEx);
	}

	public static String getUUIDFromFg(String fgFilePath) {

		Path path = Paths.get("demoData/metadata/fieldGroups");
		Charset _charset = Charset.forName("utf-8");

		try (BufferedReader reader = Files.newBufferedReader(path, _charset)) {
			String line = null;
			while ((line = reader.readLine()) != null) {
				JSONObject fg = JSON.parseObject(line);
				String fgId = fg.getString("id");

				fgId = fgId.replace("windfarm_", "");
				String uuid = fgId.substring(0, 5);
				return uuid;
			}
		} catch (Exception e) {

			e.printStackTrace();
			return null;
		}
		return null;
	}

}
